package com.youhome.service.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by chen365 on 2017/11/29.
 */
public class StringUtil {

    /**
     * 查询字符串在数组中的位置
     *
     * @param arrays 字符串数组
     * @param key    需要查找的字符串
     * @return -1表示数组中不存在查找的字符串,否则返回字符串所在位置
     */
    public static int getPosition(String[] arrays, String key) {
        int i = 0;
        for (String item : arrays) {
            if (item.equals(key)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public static String getMarital(String val) {
        String marital = "";
        if (val.equals("0")) {
            marital = "保密";
        } else if (val.equals("1")) {
            marital = "已婚";
        } else if (val.equals("2")) {
            marital = "未婚";
        } else if (val.equals("3")) {
            marital = "离异";
        }
        return marital;
    }

    /**
     * 注册来源 转换
     *
     * @param source 1.分享注册,2.顾问创建,3.B端注册,4.C端注册
     * @return
     */
    public static String getSourceText(Integer source) {
        String sourceText = "C端注册";
        switch (source) {
            case 1: {
                sourceText = "分享注册";
                break;
            }
            case 2: {
                sourceText = "顾问创建";
                break;
            }
            case 3: {
                sourceText = "B端注册";
                break;
            }
            case 4: {
                sourceText = "C端注册";
                break;
            }
            default: {
                sourceText = "C端注册";
            }
        }
        return sourceText;
    }

    /**
     * 排序字段转为数据字段
     *
     * @param filed
     * @return
     */
    public static String transitionOrderBy(String filed) {

        //bedrooms：卧室数量;bathrooms：浴数量;yearBuilt：房龄;area：面积;listPrice:价格
        switch (filed) {
            case "yearBuilt":
                filed = "year_built";
                break;
            case "listPrice":
                filed = "list_price";
                break;
            case "livingArea":
                filed = "living_area";
                break;
            case "bathroomCount":
                filed = "bathroom_count";
                break;
            case "bedroomCount":
                filed = "bedroom_count";
                break;
            default:
                filed = "year_built";
                break;
        }
        return filed;
    }

    public static String getSex(String val) {
        String sex = "";
        if (val.equals("0")) {
            sex = "女";
        } else if (val.equals("1")) {
            sex = "男";
        }
        return sex;
    }

    /**
     * 首字母转大小写
     *
     * @param s
     * @return
     */
    public static String toUpperCaseIndex(String s) {
        s = s.toLowerCase();
        char[] cs = s.toCharArray();
        // 先将第一个下标的字符转换成大写
        cs[0] = Character.toUpperCase(cs[0]);
        for (int i = 0; i < cs.length - 1; i++) {
            if (cs[i] == ' ') {
                cs[i + 1] = Character.toUpperCase(cs[i + 1]);
            }
        }
        return String.valueOf(cs);
    }

    /**
     * 生成随机文件名：当前年月日时分秒+五位随机数
     *
     * @return
     */
    public static String getDateRandomName() {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数

        return str + rannum;// 当前时间
    }

    public static String replaceBlankAndComma(String name) {
        return name
                .trim()
                .toLowerCase()
                .replaceAll("[^0-9|a-z]", "_")
                .replaceAll("_{2,}", "_");
    }
    public static String replaceBlankAndComma2(String name) {
        return name.replaceAll("-|\\. |\\.|, |,| ", "_").toLowerCase();
    }

    public static String getZip(List<String> zips, String zip) {
        Integer a = Integer.valueOf(zip);
        Integer nearest = null; //最接近的Zip
        int i = 0;
        for (String number : zips) {
            if (i == 0) {
                nearest = Integer.valueOf(number);
            } else {
                if ((Math.abs(a - nearest)) >= (Math.abs(a - Integer.valueOf(number)))) {
                    nearest = Integer.valueOf(number);
                }
            }
            i++;
        }
        return addZeroForNum(nearest.toString(), 5);
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
}
